package MANA;

import java.awt.Image;
import java.net.URL;

import javax.swing.ImageIcon;

/**
 * Class that represents any kind of Actor in the game
 * @author daan :-)
 *
 */
public abstract class Actor {
	private final int SPACE = 20;
	
	private int x;
	private int y;
	private Image image;
	
	/**
	 * creates a new actor at x, y
	 * called from constructor derived classes
	 * @param x x pos of the actor to create
	 * @param y y pos of the actor to create
	 */
	public Actor(int x, int y) {
		this.x = x;
		this.y = y;
	}
	
	/**
	 * Loads an image</br>
	 * <code>testing</code>
	 * Loads from the same dir as the classes dir
	 * @param img path to the image to load
	 */
	public void loadImage(String img) {
		try {
			URL loc = this.getClass().getResource(img);
			ImageIcon iia = new ImageIcon(loc);
			Image image = iia.getImage();
			if (image == null) {
				throw new Exception("Could not find image");
			}
			this.setImage(image);
		} catch (Exception e) {
			System.err.println("Error: could not find image " + img);
		}
	}
	
	/**
	 * Returns the image of the actor
	 * @return image of the Actor
	 */
	public Image getImage() {
		return this.image;
	}
	public void setImage(Image img) {
		image = img;
	}
	//get and set x and y ---------------------------------
	public int x() {
		return this.x;
	}
	public int y() {
		return this.y;
	}
	public void setX(int x) {
		this.x = x;
	}
	public void setY(int y) {
		this.y = y;
	}
	//-----------------------------------------------------
	
	//check for left, right, up and down collision --------
    public boolean isLeftCollision(Actor actor) {
        if (((this.x() - SPACE) == actor.x()) &&
            (this.y() == actor.y())) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isRightCollision(Actor actor) {
        if (((this.x() + SPACE) == actor.x())
                && (this.y() == actor.y())) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isTopCollision(Actor actor) {
        if (((this.y() - SPACE) == actor.y()) &&
            (this.x() == actor.x())) {
            return true;
        } else {
            return false;
        }
    }

    public boolean isBottomCollision(Actor actor) {
        if (((this.y() + SPACE) == actor.y())
                && (this.x() == actor.x())) {
            return true;
        } else {
            return false;
        }
    }
}
